package com.example.gaojt.androiddemo.boostDemo.aa;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import junit.framework.Assert;

public class BoostScanEngine {

    private Handler mHandler;
    private BoostScanCallback mBoostScanCallback;

    /*
     * Scan engine callback interface
     */
    public interface BoostScanCallback {
        void onScanStart();

        void onScanError();

        void onScanProgress(ProcessModel data);

        void onScanPreFinish(BoostResult results);

        void onScanFinish(BoostResult results);
    }


    public Handler getHandler() {
        if (mHandler == null) {
            mHandler = new Handler(Looper.getMainLooper());
        }
        return mHandler;
    }

    public void scan(final Context context) {
        final Handler handler = getHandler();
        notifyScanStart();
        new Thread() {
            @Override
            public void run() {
                BoostScanTask scanTask = new BoostScanTask(context);
                scanTask.scan(new BoostScanTask.IScanTaskCallback() {

                    @Override
                    public void onScanError() {
                        handlerScanError(handler);
                    }

                    @Override
                    public void onScanProgress(ProcessModel data) {
                        handlerScanProgress(handler, data);
                    }

                    @Override
                    public void onScanPreFinish(BoostResult results) {
                        handlerScanPreFinish(handler, results);
                    }

                    @Override
                    public void onScanFinish(BoostResult results) {
                        handlerScanFinish(handler, results);
                    }
                });
            }
        }.start();

    }

    public void setBoostScanCallback(BoostScanCallback boostScanCallback) {
        mBoostScanCallback = boostScanCallback;
    }

    private void handlerScanError(Handler handler) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                notifyScanError();
            }
        });
    }

    private void handlerScanProgress(Handler handler, final ProcessModel data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                notifyScanProgress(data);
            }
        });
    }

    private void handlerScanPreFinish(Handler handler, final BoostResult results) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                notifyScanPreFinish(results);
            }
        });
    }

    private void handlerScanFinish(Handler handler, final BoostResult results) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                notifyScanFinish(results);
            }
        });
    }

    private void notifyScanStart() {
        checkIsMainThread();
        if (mBoostScanCallback != null) {
            mBoostScanCallback.onScanStart();
        }
    }

    private void checkIsMainThread() {
        Assert.assertTrue(Thread.currentThread() == Looper.getMainLooper().getThread());
    }

    private void notifyScanError() {
        checkIsMainThread();
        if (mBoostScanCallback != null) {
            mBoostScanCallback.onScanError();
        }
    }

    private void notifyScanProgress(ProcessModel data) {
        checkIsMainThread();
        if (mBoostScanCallback != null) {
            mBoostScanCallback.onScanProgress(data);
        }
    }

    private void notifyScanPreFinish(BoostResult results) {
        checkIsMainThread();
        if (mBoostScanCallback != null) {
            mBoostScanCallback.onScanPreFinish(results);
        }
    }

    private void notifyScanFinish(BoostResult results) {
        checkIsMainThread();
        if (mBoostScanCallback != null) {
            mBoostScanCallback.onScanFinish(results);
        }
    }

}
